//
//  SMHSettingViewModel.swift
//  SMHCommerce
//
//  Created by lvzhao on 2020/6/5.
//  Copyright © 2020 lvzhao. All rights reserved.
//

import UIKit

class SMHSettingViewModel: LZBaseViewModel {

    
    override init() {
        super.init()
        
        loadData()
    }
 
    
    func loadData()  {
    
         let titles : [String] = ["修改昵称","修改登录密码","清除缓存","关于我们"]
         for title in titles{
             var settingModel = SMHSettingModel.init(title: title, detail: "", isNext: true)
             
             switch title {
             case "清除缓存":
                 settingModel.detail = "0.00M"
                 settingModel.isNext = false
                 
             case "关于我们":
                 let verison : String  = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
                 let build : String = Bundle.main.infoDictionary?["CFBundleVersion"] as! String

                 settingModel.detail = "\(verison)(\(build))"
                 
             default:
                 break
             }
             self.dataArray.append(settingModel)
         }
         
         self.floderSizeAtPath { (cachesSize) in
             
            var ramSetModel = self.dataArray[2] as! SMHSettingModel;
            let caches :String = String.init(format:"%.2fM", cachesSize)
            ramSetModel.detail = caches
            self.dataArray[2] = ramSetModel
            
            print(self.dataArray)
            
         }
     }
    
    //异步计算整个目录缓存大小
       func floderSizeAtPath(completion:@escaping (( _ fileSize:CGFloat)->Void)){
           DispatchQueue.global().async {
               var fileSize = CGFloat(0)
               if let floderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first {
                   let fineManager = FileManager.default
                   if fineManager.isExecutableFile(atPath: floderPath) {
                       if let child = fineManager.subpaths(atPath: floderPath) {
                           var size = CGFloat(0)
                           for path in child {
                               let subStr = "\(floderPath)/\(path)"
                               let aSize =  self.fileSizeAtPath(filePath: subStr)
                               size = size + aSize
                           }
                           let allSize = size/(1024 * 1024)
                           fileSize = allSize
                       }else{
                           fileSize = CGFloat(0)
                       }
                   }else {
                       fileSize = CGFloat(0)
                   }
               }else{
                   fileSize = CGFloat(0)
               }
               DispatchQueue.main.async {
                   completion(fileSize)
               }
           }
       }
       func fileSizeAtPath(filePath:String) -> CGFloat{
           let manager = FileManager.default
           if manager.isExecutableFile(atPath: filePath) {
               return CGFloat(0)
           }
           if let dic:[FileAttributeKey : Any] = try? manager.attributesOfItem(atPath: filePath) as  [FileAttributeKey : Any] {
               if let size =  dic[FileAttributeKey.size] as? Double {
                   return CGFloat(size)
               }
           }
           
        
           return CGFloat(0)
       }
        
    func clearCache(completion:@escaping (()->Void)){
          DispatchQueue.global().async {
              if let floderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first {
                  let fineManager = FileManager.default
                  if fineManager.isExecutableFile(atPath: floderPath) {
                      if  let subpaths  =  try?fineManager.contentsOfDirectory(atPath:floderPath) {
                          for subPath in subpaths {
                              let filePath = "\(floderPath)/\(subPath)"
                              try?fineManager.removeItem(atPath: filePath)
                          }
                      }
                  }
              }
              DispatchQueue.main.async {
                
                  completion()
              }
          }
          let storage = HTTPCookieStorage.shared
          if let cookies =  storage.cookies {
              for cookie  in cookies {
                  storage.deleteCookie(cookie)
              }
          }
          let urlCache = URLCache.shared
          urlCache.removeAllCachedResponses()
          urlCache.diskCapacity = 0
          urlCache.memoryCapacity  = 0
      
      }
    
}

       
  
    
    
 
    
    
   


   

/**
 
 
 */
